package com.epro.dx.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.util.ImageLoaderUtils;
import com.epro.dx.widget.HackyViewPager;
import com.epro.phoneview.PhotoView;
import com.epro.phoneview.PhotoViewAttacher.OnPhotoTapListener;
import com.epro.phoneview.PhotoViewAttacher.OnViewTapListener;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class LargeImageBrowseActivity extends Activity {
	private ViewPager mViewPager;
	private TextView selectNum;
	private TextView totalNum;
	private String[] photoUrls;
	private int currentIndex;
	
	private int screenWidth;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_lagerimage);
		Intent intent=getIntent();
		if(null==intent){
			finish();
			return;
		}
		photoUrls=intent.getStringArrayExtra("PhotoUrls");
		currentIndex=intent.getIntExtra("SelectedIndex", 1);
		screenWidth=intent.getIntExtra("ScreenWidth", 480);
		intView();
		mViewPager.setAdapter(new ImageViewPagerAdapter(photoUrls));
		mViewPager.setCurrentItem(currentIndex);
		totalNum.setText(photoUrls.length+"");
		selectNum.setText(currentIndex+1+"/");
		
	}
	
	private void intView(){
		mViewPager=(HackyViewPager)findViewById(R.id.large_image_viewpager);
		selectNum=(TextView) findViewById(R.id.product_select_num);
		totalNum=(TextView) findViewById(R.id.product_img_num);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				totalNum.setText(photoUrls.length+"");
				selectNum.setText(position+1+"/");
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
//		RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,screenWidth);
////		params.height=android.support.v4.view.ViewPager.LayoutParams.MATCH_PARENT;
////		params.width=screenWidth;
//		params.addRule(RelativeLayout.CENTER_IN_PARENT ,-1);
//		mViewPager.setLayoutParams(params);
	}

	private class ImageViewPagerAdapter extends PagerAdapter {

		private String[] imageUrls;
		
		private DisplayImageOptions options;
		
//		private onSingleClickListenner singleClickListenner;
		
		public ImageViewPagerAdapter(String[] imageUrls){
			this.imageUrls=imageUrls;
//			this.singleClickListenner=singleClickListenner;
			options = ImageLoaderUtils.getImageLoaderOptions(
					R.drawable.default488_488, R.drawable.default488_488);
		}
		@Override
		public int getCount() {
			return imageUrls.length;
		}
		
//		private interface onSingleClickListenner{
//			void onSingleClick();
//		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			ViewPager.LayoutParams params = new ViewPager.LayoutParams();
			params.height=screenWidth;
			params.width=screenWidth;
			PhotoView photoView = new PhotoView(container.getContext());
			photoView.setLayoutParams(params);
			String filePath = imageUrls[position];
			if (!TextUtils.isEmpty(filePath) && null != photoView) {
				photoView.setTag(filePath);
				// imageWorker.loadImage(filePath, imageView);
				ImageLoader.getInstance()
						.displayImage(filePath, photoView, options);
			} else {
				photoView.setImageResource(R.drawable.default488_488);

			}
//			OnViewTapListener
			photoView.setOnViewTapListener(new OnViewTapListener() {
				
				@Override
				public void onViewTap(View arg0, float arg1, float arg2) {
					LargeImageBrowseActivity.this.finish();
				}
			});
			photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
				
				@Override
				public void onPhotoTap(View arg0, float arg1, float arg2) {
					LargeImageBrowseActivity.this.finish();
				}
			});
			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			ViewPager.LayoutParams params = new ViewPager.LayoutParams();
			params.height=screenWidth;
			params.width=screenWidth;
			((View) object).setLayoutParams(params);
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}
}
